﻿/*-------------------------------------------------------------------------
* 命名空间: WaferSelectV3.ViewModels.DialogViewModels/SetParametersViewModel
* 类       名: SetParametersViewModel
* 功      能:  TD
* 时      间:  2024-05-10 17:32:06
* 版      本:  V1.0
* 作      者:  Fioman
* 格      言:  Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WaferSelectV3.Common;
using WaferSelectV3.DAL.DataEntity;
using WaferSelectV3.EventsManager;
using WaferSelectV3.Models;
using WaferSelectV3.ViewModels.SetParamsViewModels;
using WaferSelectV3.Views.SetParamsPages;

namespace WaferSelectV3.ViewModels.DialogViewModels
{
    public class SetParametersViewModel : BindableBase, IDialogAware
    {
        private IRegionManager _regionManager;
        private IEventAggregator _eventAggregator;
        public SetParametersViewModel(IRegionManager regionManager, IEventAggregator eventAggregator)
        {
            _regionManager = regionManager;
            _eventAggregator = eventAggregator;
        }

        private string? _title;

        public string? Title
        {
            get { return _title; }
            set
            {
                _title = value;
                RaisePropertyChanged();
            }
        }


        private ObservableCollection<MenuBar>? _settingMenuBars;
        /// <summary>
        /// 设置菜单栏
        /// </summary>
        public ObservableCollection<MenuBar>? SettingMenuBars
        {
            get { return _settingMenuBars; }
            set
            {
                _settingMenuBars = value;
                RaisePropertyChanged();
            }
        }


        private MenuBar? _settingMenuBarSelected;

        public MenuBar? SettingMenuBarSelected
        {
            get { return _settingMenuBarSelected; }
            set
            {
                _settingMenuBarSelected = value;
                RaisePropertyChanged();
            }
        }



        public event Action<IDialogResult>? RequestClose;

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
            // 发布窗口关闭事件,释放对应的资源
            _eventAggregator.GetEvent<SetDialogCloseEvent>().Publish();
            SettingMenuBars?.Clear();
            SettingMenuBars = null;
            SettingMenuBarSelected = null;
        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            SettingMenuBars = new ObservableCollection<MenuBar>()
            {
                new MenuBar(){Title="通信设置",ViewName=nameof(SlaveSettingsView),Icon="\uE928"},
                new MenuBar(){Title="工作参数",ViewName=nameof(SlaveSettingsView),Icon="\uE662"},
            };
            SettingMenuBarSelected = SettingMenuBars.FirstOrDefault();
            // 1. 这种用法需要将Region添加到Regions集合中去才可以使用
            //_regionManager.Regions[RegionNameManager.SetParamsContent].RequestNavigate(nameof(SlaveSettingsView));
            // 2. 而这种用法就不需要添加到Regions集合中去
            _regionManager.RequestNavigate(RegionNameManager.SetParamsContent, nameof(SlaveSettingsView));
            if (parameters != null)
            {
                parameters.TryGetValue("Title", out string title);
                if (!string.IsNullOrEmpty(title))
                {
                    Title = title;
                }
            }
        }
    }
}
